package com.electronsys.dept.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.electronsys.dept.dao.IDeptDAO;
import com.electronsys.pojo.Department;
import com.electronsys.pojo.Employee;
import com.electronsys.pojo.Log;
import com.electronsys.pojo.Module;
import com.electronsys.pojo.ModuleContent;

public class DeptDAOImpl extends HibernateDaoSupport implements IDeptDAO {

	public List<Department> deptList() {
		HibernateTemplate htmp = this.getHibernateTemplate();
		String hql = "from Department";
		List<Department> dlist = htmp.find(hql);
		return dlist;
	}

	public void addDept(Department dept) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		htmp.save(dept);
	}

	public List<Department> deptList(Integer deptId) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		String hql = "from Department where dept_id = ?";
		List<Department> dlist = htmp.find(hql,deptId);
		return dlist;
	}

	public boolean updateDept(Department dept) {
		System.out.println("update dao");
		HibernateTemplate htmp = this.getHibernateTemplate();
		try{
			htmp.update(dept);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public void deleteDept(Integer[] deptId) {
		HibernateTemplate htmp	= this.getHibernateTemplate();
		for(int i=0; i<deptId.length;i++){
			Department dept = htmp.get(Department.class, deptId[i]);
			htmp.delete(dept);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Department> deptListByPage(final Integer start, final Integer max) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		final String hql = "select d from Department d order by dept_id";
		List<Department> dlist = htmp.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
			Query query = session.createQuery(hql);
			query.setFirstResult(start);
			query.setMaxResults(max);
			List list = query.list();
			return list;
			}
		});

		return dlist;
	}

	public List<Employee> empList(Integer deptId) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		String hql = "from Employee where emp_deptid = ?";
		List<Employee> elist = htmp.find(hql,deptId);
		return elist;
	}
	
	public List<Employee> empMessList(Integer empId) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		String hql = "from Employee where emp_id = ?";
		List<Employee> elist = htmp.find(hql,empId);
		return elist;
	}

	public void addDeptLog(Log log) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		htmp.save(log);
	}

	public List<Log> logList(Integer empId) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		String hql = "from Log where log_empid = ?";
		List<Log> llist = htmp.find(hql,empId);
		return llist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Log> logListByPage(final Integer start, final Integer max) {
		HibernateTemplate htmp = this.getHibernateTemplate();
		final String hql = "select l from Log l order by log_date desc";
		List<Log> llist = htmp.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
			Query query = session.createQuery(hql);
			query.setFirstResult(start);
			query.setMaxResults(max);
			List list = query.list();
			return list;
			}
		});

		return llist;
	}

	public List<Employee> empList() {
		HibernateTemplate htmp = this.getHibernateTemplate();
		String hql = "from Employee";
		List<Employee> elist = htmp.find(hql);
		return elist;
	}

}
